﻿@using RTW.Model
@model PersonalSuSongDirectory
@{
    Layout = "~/Views/Shared/_Layout.cshtml";
 ViewData["Title"] = "湖北省检察院档案管理系统 - 个人 -我的诉讼档案-目录编写";
}

<div class="Direcctory-body">
    <table class="layui-table" id="table1" lay-filter="table1"></table>

    <div class="layui-form-item  layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="确认添加">
    </div>
    <input type="hidden" id="fileid" name="fileid" value="@ViewData["fileid"]" />

</div>
<script type="text/html" id="uploadingbox">
    <div id="">
        <div class="Direcctory-body" id="">
            <fieldset class="layui-elem-field">
                <legend>条目数据挂接</legend>
                <div class="layui-field-box">
                    <div class="layui-row">
                        <div class="layui-col-xs8 layui-col-xs-offset2">
                            <label class="layui-form-label">选择Excel:</label>
                            <div class="layui-inline Direcctory-wjsc">
                                <button class="layui-btn layui-btn-sm" id="selectefile">选择文件</button>
                                <text>未选择任何文件</text>
                            </div>
                            <button class="layui-btn layui-btn-sm" id="putfile">上传文件</button>
                        </div>
                    </div>
                </div>
                <div class="layui-field-box">
                    <p class="orange">选择模板下载，然后编辑上传。上传时，文件名不可进行修改，格式为：XXXXXX目录.xls，模板内红色栏位为必填字段</p>

                    <hr>
                </div>
                <div class="layui-field-box">
                    <textarea class="layui-textarea Directory-textarea"></textarea>
                </div>
            </fieldset>
        </div>
    </div>
</script>
<script type="text/html" id="tooltop">
    <div class="layui-row Directory-row">
        <div class="layui-col-sm7" style="visibility:hidden;">
            <span class="orange">提示：在表格上点击右键可 <b>删除当前行</b> 、 <b>向上插入行</b>、<b>向下插入行</b>  操作！</span>
        </div>
        <div class="layui-col-sm4 layui-col-sm-offset1">
            <div class="layui-btn-group">
                <a class="layui-btn layui-btn-sm" style="visibility:hidden;" href="~/layuiadmin/Template/诉讼档案卷内目录.xls" download="诉讼档案卷内目录.xls">导入模板下载</a>
                <button type="button" style="visibility:hidden;" class="layui-btn layui-btn-sm" lay-event="uploading">导入</button>
                <button type="button" class="layui-btn layui-btn-sm" lay-event="addrow">添加行</button>
                <button type="button" class="layui-btn layui-btn-sm" lay-event="delrow">缩减行</button>
            </div>
        </div>
    </div>
</script>


@section scripts{
    <script>
        layui.config({
            base: '/layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['form', 'index', 'table', 'laydate', 'layer', 'jquery', 'upload'], function () {
            var form = layui.form, index = layui.index, table = layui.table, laydate = layui.laydate, layer = layui.layer, $ = layui.$, upload = layui.upload;

            var fileid = $("#fileid").val();

            //初始化表格
            var table1 = table.render({
                elem: '#table1',
                size: 'sm',
                height: 'full-20',
                toolbar: '#tooltop',
                defaultToolbar: [],
                limit: 300,
                cols: [[
                    { field: 'bh', type: 'numbers', title: '#' }
                    , { field: 'ID', title: 'ID', hide: true }
                    , { field: 'DirIndex', title: '序号', width:60 , edit: true }
                    , { field: 'ArticleNo', title: '文号', width: 100, edit: true }
                    , { field: 'Responsibilityer', title: '责任者', width: 100, edit: true }
                    , { field: 'DirName', title: '文件(材料)名称', edit: true }
                    , { field: 'DirDate', title: '日期', width: 90, edit: true }
                    , { field: 'PageNoStar', title: '页码起', width: 70,  edit: true }
                    , { field: 'PageNoEnd', title: '页码止', width: 70,  edit: true }
                    , { field: 'SecretLevel', title: '密级', width: 70, edit: true }
                    , { field: 'Remark', title: '备注', width: 60, edit: true }
                ]],
                data: [{}],
                text: "对不起，加载出现异常！",
                done: function (res, curr, count) {
                    if (res.count == 0) {
                        $(".layui-table-main").html('<div class="layui-none">暂无数据</div>');
                    }
                }
            });

            //数据加载
            $(function () {
                $.ajax({
                    type: "post",
                    url: '/Personal/SuSong/GetDirectoryJson?&id=' + fileid,
                    dataType: "json",
                    success: function (msg) {
                        //获取数据，并重新加载表格
                        table.reload('table1', {
                            data: msg
                        });
                    }
                })
            });

            //表单按钮
            table.on('toolbar(table1)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'uploading':
                        top.layer.open({
                            type: '1',
                            title: '卷内目录导入',
                            area: ['700px', '450px'],
                            content: layui.$('#uploadingbox').html()
                        })
                        break;
                    case 'addrow':
                        var oldData = table.cache['table1'];
                        var data1 = { "id": "", "DirIndex": oldData.length+1, "ArticleNo": "", "Responsibilityer": "", "DirName": "", "PageNoStar": "", "PageNoEnd": "", "SecretLevel": "", "Remark": "" };
                        oldData.push(data1);
                        table.reload('table1', {
                            data: oldData
                        });
                        break;
                    case 'delrow':
                        var oldData = table.cache['table1'];
                        oldData.pop();
                        console.log(oldData)
                        table.reload('table1', {
                            data: oldData
                        });
                        break;
                };
            });

            //添加事件
            form.on('submit(layuiadmin-app-form-submit)', function (data) {
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

                var datas = layui.table.cache['table1'];//获取table数据

                $.ajax({
                    type: 'post',
                    url: '/Personal/SuSong/AddDirectoryForFileId',
                    data: { fileId: fileid,directorys: JSON.stringify(datas) },//json数据格式 
                    async: true,
                    dataType: "json",
                    success: function (res) {
                        if (res == '1') {
                            top.layer.msg("保存成功！", { icon: 6, times: 1500 });
                            parent.layer.close(index); //再执行关闭
                        } else {
                            layer.msg("保存失败！", { icon: 5, times: 1500 });
                        }
                    },
                    error: function (data) {
                        layer.msg(data.msg, { icon: 5, times: 1500 });
                    }
                });
                return false;
            });

            upload.render({
                elem: '#selectefile'
                , auto: false
                //,url:'https://httpbin.org/post'
                , accept: 'xls',
                acceptMime: 'xls'
                , bindAction: '#putfile'
                , done: function (res) {
                    layer.msg('上传成功');
                    console.log(res)
                }
            });
        });
    </script>
}